package cn.innosmart.aq.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ConnectionManager;

import org.w3c.dom.Text;

import cn.innosmart.aq.R;
import cn.innosmart.aq.isp.ISPTool;
import cn.innosmart.aq.util.EditTextCheckUtil;
import cn.innosmart.aq.util.SharedUtil;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/9/2.
 */
public class TelPushMessageActivity extends BaseActivity {
    private EditText etTel;
    private ToggleButton tbSwitchTell;
    private ConnectionEntity connectionEntity;
    private Spinner spinnerPhonePrefix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tel_push_message);
        connectionEntity = ConnectionManager.getInstance().getCurrentConnectionEntity();
        etTel = (EditText) findViewById(R.id.et_tel);

        spinnerPhonePrefix = (Spinner) findViewById(R.id.spinner_phone_prefix_push);

        String fullTelNo = SharedUtil.getInstance().readTelNo(connectionEntity.getUid());
        System.out.println("fullTelNo:" + fullTelNo);
        String prefix = null;
        String telNo = null;
        if (fullTelNo != null && fullTelNo.contains("|||")) {
            prefix = fullTelNo.split("\\|\\|\\|")[0];
            telNo = fullTelNo.split("\\|\\|\\|")[1];
        } else {
            telNo = fullTelNo;
        }

        if (prefix != null) {
            if (prefix.contains("+086")) {
                spinnerPhonePrefix.setSelection(0);
            } else if (prefix.contains("+886")) {
                spinnerPhonePrefix.setSelection(1);
            }
        }

        if (telNo != null) {
            etTel.setText(telNo);
        } else {
            etTel.setText(SystemConstant.username);
        }

        tbSwitchTell = (ToggleButton) findViewById(R.id.tb_switch_tel);
        setBar();

        tbSwitchTell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = spinnerPhonePrefix.getSelectedItemPosition();
                String prefix = "+086";
                if (p == 0) {
                    prefix = "+086";
                } else if (p == 1) {
                    prefix = "+886";
                }
                final String prefixFinal = prefix;
                final String telNo = etTel.getText().toString().trim();
                if (tbSwitchTell.isChecked()) {
                    if (!EditTextCheckUtil.checkTelNo(prefix, telNo)) {
                        showToast(getString(R.string.input_telno_wrong));
                        tbSwitchTell.setChecked(false);
                        return;
                    }

                    ISPTool.getInstance().updateISP("tel", connectionEntity.getUid(), "yes", prefix, telNo, new ISPTool.onISPCallBack() {
                        @Override
                        public void onSuccess(int resultcode, int xingeId) {
                            SharedUtil.getInstance().writeTelStatus(true, connectionEntity.getUid());
                            SharedUtil.getInstance().writeTelNo(prefixFinal + "|||" + telNo, connectionEntity.getUid());
                            showToast(getString(R.string.subscribe_success));
                            mHandler.sendEmptyMessage(LOCK);
                        }

                        @Override
                        public void onFailed(int resultcode, int xingeId) {
                            mHandler.sendEmptyMessage(SUBSCRIBEERROR);
                        }
                    });

                } else {
                    ISPTool.getInstance().updateISP("tel", connectionEntity.getUid(), "no", prefix, telNo, new ISPTool.onISPCallBack() {
                        @Override
                        public void onSuccess(int resultcode, int xingeId) {
                            SharedUtil.getInstance().writeTelStatus(false, connectionEntity.getUid());
                            showToast(getString(R.string.unsubscribe_success));
                            mHandler.sendEmptyMessage(UNLOCK);
                        }

                        @Override
                        public void onFailed(int resultcode, int xingeId) {
                            mHandler.sendEmptyMessage(UNSUBSCRIBEERROR);
                        }
                    });
                }
            }
        });
    }

    private void checkInput() {

    }

    @Override
    protected void onResume() {
        if (SharedUtil.getInstance().readTelStatus(connectionEntity.getUid())) {
            etTel.setEnabled(false);
            tbSwitchTell.setChecked(true);
        } else {
            etTel.setEnabled(true);
            tbSwitchTell.setChecked(false);
        }
        setBaseDialogContext(this);
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    /**
     * 设置界面Toolbar
     */
    private Toolbar toolbar;

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        toolbar.setTitle("");
        ((TextView) findViewById(R.id.tv_toolbar)).setText(getString(R.string.activity_tel_title_set));
        setSupportActionBar(toolbar);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mHandler.sendEmptyMessage(EXIT);
//            }
//        });
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mDrawer.openDrawer(GravityCompat.START);
                mHandler.sendEmptyMessage(EXIT);
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private final int EXIT = 0;
    private final int LOCK = 1;
    private final int UNLOCK = 2;
    private final int SUBSCRIBEERROR = -1;
    private final int UNSUBSCRIBEERROR = -2;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
                case LOCK:
                    etTel.setEnabled(false);
                    break;
                case UNLOCK:
                    etTel.setEnabled(true);
                    break;
                case SUBSCRIBEERROR:
                    showToast(getString(R.string.subscribe_error));
                    tbSwitchTell.setChecked(false);
                    break;
                case UNSUBSCRIBEERROR:
                    showToast(getString(R.string.unsubscribe_error));
                    tbSwitchTell.setChecked(true);
                    break;
            }
        }
    };

}
